/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mobile;

import domain.Mobileconsumption;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import turnover.Company;
import util.Time;

/**
 *
 * @author Alberto
 */
public class NewFile {

    private List<Call> callList = new LinkedList();
    private List<Message> messList = new LinkedList();
    private File file;
    private Mobileconsumption mc;

    public NewFile(File file, Mobileconsumption mc) throws IOException {
        this.file = file;
        this.mc = mc;
        if (mc.getInvoice().getCompany().equals("Orange")) {
            this.consumoOrange();
        } else if (mc.getInvoice().getCompany().equals("Vodafone")) {
            this.consumoVodafone();
        }
    }

    private Vector leeArchivo(File file) throws IOException {
        Vector informacionArchivo = new Vector();
        POIFSFileSystem poifsFileSystem = null;
        try {
            poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        HSSFWorkbook hssfWorkbook = null;
        try {
            hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
        Iterator iterator = hssfSheet.rowIterator();
        while (iterator.hasNext()) {
            HSSFRow hssfRow = (HSSFRow) iterator.next();
            Vector informacionFila = new Vector();
            for (int i = hssfRow.getFirstCellNum(); i < hssfRow.getLastCellNum(); i++) {
                HSSFCell hssfCell = hssfRow.getCell(i);
                if (hssfCell != null) {
                    switch (hssfCell.getCellType()) {
                        case HSSFCell.CELL_TYPE_BLANK:
                            informacionFila.add("");
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN:
                            informacionFila.add(hssfCell.getBooleanCellValue());
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA:
                            informacionFila.add(hssfCell.getStringCellValue());
                            break;
                        case HSSFCell.CELL_TYPE_NUMERIC:
                            informacionFila.add(hssfCell.getNumericCellValue());
                            break;
                        case HSSFCell.CELL_TYPE_STRING:
                            informacionFila.add(hssfCell.getStringCellValue());
                            break;
                        default:
                    }
                }
            }
            informacionArchivo.add(informacionFila);
        }
        return informacionArchivo;
    }

    private void consumoOrange() throws IOException {
        Vector vector = leeArchivo(file);
        Date date = null;
        for (int fila = 5; fila < vector.size() - 8; fila++) {
            int i = 0;
            Call call = null;
            Message mess = null;
            date = null;
            Time time = null;
            Company company = null;
            Long mobile = null;
            Time duration = null;
            Double cost = null;
            String tipo = "";
            Vector v = (Vector) vector.get(fila);
            Iterator it2 = v.iterator();
            while (it2.hasNext()) {
                String o = null;
                if (i == 5) {
                    cost = (Double) it2.next();
                } else {
                    o = (String) it2.next();
                }
                if (i == 0) {
                    date = this.creaFecha(o);
                } else if (i == 1) {
                    mobile = Long.parseLong(o);
                } else if (i == 2) {
                    tipo = o;
                } else if (i == 3) {
                    time = this.creaHora(o);
                } else if (i == 4) {
                    duration = this.creaDurationOra(o);
                } else if (i == 6) {
                    company = new Company(o, 0);
                }
                i++;
            }
            Date dateTo = new Date(mc.getDateTo().getYear(), mc.getDateTo().getMonth(), mc.getDateTo().getDay());
            Date dateFrom = new Date(mc.getDateFrom().getYear(), mc.getDateFrom().getMonth(), mc.getDateFrom().getDay());
            if (tipo.equals("VOZ") && date.compareTo(dateFrom) >= 0 && date.compareTo(dateTo) <= 0) {
                call = new Call(company, mobile, duration, time, date, cost);
                callList.add(call);
            } else if (tipo.equals("SMS") && date.compareTo(dateFrom) >= 0 && date.compareTo(dateTo) <= 0) {
                mess = new Message(mobile, time, date);
                messList.add(mess);
            }
        }
    }

    private void consumoVodafone() throws IOException {
        Vector vector = this.leeArchivo(file);
        Iterator it1 = vector.iterator();
        Date date = null;
        boolean saltarFila = true;
        while (it1.hasNext()) {
            int i = 0;
            Call call = null;
            Message mess = null;
            date = null;
            Time time = null;
            Company company = null;
            Long mobile = null;
            Time duration = null;
            Double cost = null;
            String tipo = "";
            Vector v = (Vector) it1.next();
            Iterator it2 = v.iterator();
            if (!saltarFila) {
                while (it2.hasNext()) {
                    String o = (String) it2.next();
                    if (i == 0) {
                        date = this.creaFecha(o);
                        time = this.creaHora(o);
                    } else if (i == 1) {
                        tipo = o;
                    } else if (i == 2) {
                        if (tipo.equals("SMS") || tipo.equals("Llamadas Voz")) {
                            mobile = Long.parseLong(o);
                        }
                    } else if (i == 3) {
                        if (tipo.equals("Llamadas Voz")) {
                            duration = this.creaDurationVod(o);
                        }
                    } else if (i == 4) {
                        if (!o.equals("Vodafone") && !o.contains("Fijo nacional")) {
                            company = new Company("Otro", 0);
                        } else {
                            company = new Company(o, 0);
                        }
                    } else if (i == 5) {
                        int index = o.indexOf(",");
                        o = o.substring(1, index) + "." + o.substring(index + 1);
                        cost = Double.parseDouble(o);
                    }
                    i++;
                }
                Date dateTo = new Date(mc.getDateTo().getYear(), mc.getDateTo().getMonth(), mc.getDateTo().getDay());
                Date dateFrom = new Date(mc.getDateFrom().getYear(), mc.getDateFrom().getMonth(), mc.getDateFrom().getDay());
                if (tipo.equals("Llamadas Voz") && date.compareTo(dateFrom) >= 0 && date.compareTo(dateTo) <= 0) {
                    call = new Call(company, mobile, duration, time, date, cost);
                    this.callList.add(call);
                } else if (tipo.equals("SMS") && date.compareTo(dateFrom) >= 0 && date.compareTo(dateTo) <= 0) {
                    mess = new Message(mobile, time, date);
                    this.messList.add(mess);
                }
            }
            saltarFila = false;
        }
    }

    private Date creaFecha(String o) throws IOException {
        Date date = null;
        if (o.substring(2, 3).equals("/") && o.substring(5, 6).equals("/")) {
            int day = Integer.parseInt(o.substring(0, 2));
            int month = Integer.parseInt(o.substring(3, 5));
            int year = Integer.parseInt(o.substring(6, 10));
            date = new Date(year, month, day);
        }
        return date;
    }

    public Time creaHora(String o) {
        Time time = null;
        int hour = Integer.parseInt(o.substring(0, 2));
        int minute = Integer.parseInt(o.substring(3, 5));
        int second = Integer.parseInt(o.substring(6, 8));
        time = new Time(hour, minute, second);
        return time;
    }

    public Time creaDurationOra(String o) {
        Time time = null;
        int hour = Integer.parseInt(o.substring(0, 1));
        int minute = Integer.parseInt(o.substring(4, 6));
        int second = Integer.parseInt(o.substring(10, 12));
        time = new Time(hour, minute, second);
        return time;
    }

    public Time creaDurationVod(String o) {
        Time time = null;
        int hour = Integer.parseInt(o.substring(0, 2));
        int minute = Integer.parseInt(o.substring(3, 5));
        int second = Integer.parseInt(o.substring(6, 8));
        time = new Time(hour, minute, second);
        return time;
    }

    public List<Call> getCallList() {
        return callList;
    }

    public List<Message> getMessList() {
        return messList;
    }
}
